package org.atovio.coolexam.controller;

import org.atovio.coolexam.common.Result;
import org.atovio.coolexam.entity.Question;
import org.atovio.coolexam.entity.QuestionOptions;
import org.atovio.coolexam.entity.Subject;
import org.atovio.coolexam.service.QuestionOptionsService;
import org.atovio.coolexam.service.QuestionService;
import org.atovio.coolexam.service.SubjectService;
import org.atovio.coolexam.viewmodel.admin.question.OptionsQuestionFormVM;
import org.atovio.coolexam.viewmodel.admin.question.TableRowVM;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/questions")
public class QuestionController {

    private final SubjectService subjectService;
    private final QuestionService questionService;
    private final QuestionOptionsService questionOptionsService;

    @Autowired
    public QuestionController(QuestionService questionService, QuestionOptionsService questionOptionsService, SubjectService subjectService) {
        this.subjectService = subjectService;
        this.questionService = questionService;
        this.questionOptionsService = questionOptionsService;
    }

    @GetMapping("/{subjectId}")
    public Result getQuestionBySubjectId(@PathVariable Integer subjectId) {
        List<Question> questions = questionService.getSelective(Map.of("subjectId", subjectId));
        return Result.success(questions);
    }

    @PostMapping("/blank")
    public Result addBlank(@RequestBody Question question) {
        return Result.success(questionService.add(question));
    }

    @PostMapping("/judgement")
    public Result addJudgement(@RequestBody OptionsQuestionFormVM formVM) {
        return this.addOptionQuestion(formVM, 3);
    }

    @PostMapping("/multiple")
    public Result addMultiple(@RequestBody OptionsQuestionFormVM formVM) {
        return this.addOptionQuestion(formVM, 2);
    }

    @PostMapping("/single")
    public Result addSingle(@RequestBody OptionsQuestionFormVM formVM) {
        return this.addOptionQuestion(formVM, 1);
    }

    /**
     * 新增选项类题目
     *
     * @param formVM 题目具体数据
     * @param type   题型
     * @return 响应结果
     */
    private Result addOptionQuestion(@RequestBody OptionsQuestionFormVM formVM, Integer type) {
        Integer subjectId = formVM.getSubjectId();
        String content = formVM.getContent();
        String answer = formVM.getAnswer();
        Integer level = formVM.getLevel();
        Integer score = formVM.getScore();
        String optionA = formVM.getOptionA();
        String optionB = formVM.getOptionB();
        String optionC = formVM.getOptionC();
        String optionD = formVM.getOptionD();

        Question question = new Question();
        question.setSubjectId(subjectId);
        question.setContent(content);
        question.setQuestionType(type);
        question.setCorrectAnswer(answer);
        question.setScore(score);
        question.setDifficultyLevel(level);
        Question dbQuestion = questionService.add(question);

        // 判断题
        QuestionOptions questionOptions = new QuestionOptions();
        questionOptions.setQuestionId(dbQuestion.getId());
        questionOptions.setContent(optionA);
        questionOptions.setOrder(1);
        questionOptionsService.add(questionOptions);
        questionOptions.setContent(optionB);
        questionOptions.setOrder(2);
        questionOptionsService.add(questionOptions);

        // 单选题和多选题
        if (!Objects.isNull(optionC) && !Objects.isNull(optionD)) {
            questionOptions.setContent(optionC);
            questionOptions.setOrder(3);
            questionOptionsService.add(questionOptions);
            questionOptions.setContent(optionD);
            questionOptions.setOrder(4);
            questionOptionsService.add(questionOptions);
        }

        return Result.success();
    }

    @GetMapping
    public Result getExamPaperList() {
        List<TableRowVM> tableData = new ArrayList<>();
        List<Question> questions = questionService.getAll();

        questions.forEach(q -> {
            TableRowVM tableRowVM = new TableRowVM();
            Subject subject = subjectService.getById(q.getSubjectId());
            String type = switch (q.getQuestionType()) {
                case 1 -> "单选题";
                case 2 -> "多选题";
                case 3 -> "判断题";
                case 4 -> "填空题";
                case 5 -> "简答题";
                default -> "未知";
            };
            String level = switch (q.getDifficultyLevel()) {
                case 1 -> "简单";
                case 2 -> "中等";
                case 3 -> "困难";
                default -> "未知";
            };

            tableRowVM.setType(type);
            tableRowVM.setLevel(level);
            tableRowVM.setQuestion(q);
            tableRowVM.setSubject(subject);

            tableData.add(tableRowVM);
        });
        return Result.success(tableData);
    }

    @DeleteMapping("/{id}")
    public Result removeById(@PathVariable Integer id) {
        questionService.deleteById(id);
        return Result.success();
    }

}
